﻿/**
 * 编写者：张维锋
 * 时间：2011-5-26
 * */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using System.Diagnostics;
using Orf_system.收费业务;

namespace Orf_system
{
    public partial class FrmMain : Form
    {
        public FrmLogin login;
        public string emp_sn= null;
        public FrmMain()
        {
            InitializeComponent();
            SkinFile(Properties.Resources.MP10);
        }

        public void SkinFile(byte[] bytes)
        {
            MemoryStream stream = new MemoryStream(bytes);
            skinEngine1.SkinStream = stream;
        }

        /// <summary>
        /// 判断是否要打开的子窗体已经打开，如果打开了就显示在最前面，没有就打开它
        /// </summary>
        /// <param name="ChildName"></param>
        /// <returns></returns>
        private bool CheckChildFrm(string ChildName)
        {
            Form MdiChild = null;
            foreach (Form  frm in this.MdiChildren)
            {
                if(frm.GetType().ToString()==ChildName)
                {
                    MdiChild = frm;
                    break;
                }
            }

            if (MdiChild!=null)
            {
                MdiChild.TopMost = true;
                MdiChild.Show();
                MdiChild.Focus();
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 打开子窗体
        /// </summary>
        /// <param name="ChildTypeString"></param>
        private void OpenForm(string ChildTypeString)
        {
            Form myChild = null;
            if (!CheckChildFrm(ChildTypeString))             
            {                 
                // Get current process assembly                
                Assembly assembly = Assembly.GetExecutingAssembly();                 
                // Create data type using type string                 
                Type typForm = assembly.GetType(ChildTypeString);                
                // Create object using type's "InvokeMember" method               
                Object obj = typForm.InvokeMember(
                    null,                     
                    BindingFlags.DeclaredOnly |                    
                    BindingFlags.Public | 
                    BindingFlags.NonPublic |                    
                    BindingFlags.Instance | 
                    BindingFlags.CreateInstance,                    
                    null,                    
                    null,                     
                    null);                 
                // 显示子窗体          
                if (obj != null)                 
                {                     
                    myChild = obj as Form;                     
                    myChild.MdiParent = this; 
                    myChild.Show();
                    //myChild.WindowState = FormWindowState.Maximized; 
                    myChild.Focus();                 
                }            
            } 
        }

        private void tspExit_Click(object sender, EventArgs e)
        {
            if (login.action != "reLogin")
            {
                if (MessageBox.Show("您确定要退出系统吗？", "确认", MessageBoxButtons.YesNo) != DialogResult.Yes)
                {
                    this.DialogResult = DialogResult.No;
                    return;
                }
                else
                {
                    this.DialogResult = DialogResult.Yes;
                    System.Diagnostics.Process.GetCurrentProcess().Kill();
                }
            }

        }

        /// <summary>
        /// 打开退号窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tuiHao_Click(object sender, EventArgs e)
        {
         
        }

        /// <summary>
        /// 打开建立病人档案窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void createPatientFile_Click(object sender, EventArgs e)
        {
            
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            this.CurrentUser.Text = login.empName;
            this.LoginTime.Text= login.loginTime;
            emp_sn = FrmLogin.emp_sn;
        }

        /// <summary>
        ///锁定系统 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void suoping_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
            login.action = "lock";
            this.Hide();
            login.clearPassword();
            login.txtUserName.Enabled = false;
            login.Show();
            login.txtPwd.Focus();
        }

        /// <summary>
        /// 打开的子窗体层叠显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cengDie_Click(object sender, EventArgs e)
        {
            this.LayoutMdi(MdiLayout.Cascade);
        }

        /// <summary>
        /// 关闭所有子窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void closeAll_Click(object sender, EventArgs e)
        {
            foreach (Form form in this.MdiChildren)
            {
                form.Close();
            }
        }

        /// <summary>
        /// 让打开的窗体层叠显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pingPu_Click(object sender, EventArgs e)
        {
            this.LayoutMdi(MdiLayout.TileHorizontal);
        }

        /// <summary>
        /// 注销当前用户，重新登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 注销用户ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            login.action = "reLogin";
            Application.Exit();
            Process process = new Process();
            process.StartInfo.FileName = Application.ExecutablePath;
            process.Start();
        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (login.action != "reLogin")
            {
                if (MessageBox.Show("您确定要退出系统吗？", "确认", MessageBoxButtons.YesNo) != DialogResult.Yes)
                {
                    e.Cancel = true;
                    this.DialogResult = DialogResult.No;
                }
                else
                {
                    this.DialogResult = DialogResult.Yes;
                    System.Diagnostics.Process.GetCurrentProcess().Kill();
                }
            }
        }

        private void huaJiaSf_Click(object sender, EventArgs e)
        {
            OpenForm(typeof(FrmhuaJiaSf).ToString()); 
        }

        private void tuiFei_Click(object sender, EventArgs e)
        {
            OpenForm(typeof(FrmTuifei).ToString());
        }

        private void faPiaoZf_Click(object sender, EventArgs e)
        {
            OpenForm(typeof(FrmFpZf).ToString());
            
        }
    }
}
